using UnityEngine;
using RGuang.PlathformCtrl.FSM;

namespace RGuang.PlathformCtrl
{
    public class PlayerState : ScriptableObject, IState
    {
        [SerializeField] string stateName;
        [SerializeField, Range(0f, 1f)] float transitionDuration = 0.1f;//动画过度时长
        protected int stateHash;

        protected Animator animator;
        protected PlayerInput input;
        protected PlayerController player;
        protected PlayerStateMachine stateMachine;

        float stateStartTime;//状态开始时间
        protected float currentSpeed;

        protected float stateDuration => Time.time - stateStartTime;//当前状态已存在时长
        protected bool isAnimationFinished => stateDuration >= animator.GetCurrentAnimatorStateInfo(0).length;//动画是否播放完成
        void OnEnable()
        {
            stateHash = Animator.StringToHash(stateName);
        }

        public void Initialize(PlayerController player, Animator animator, PlayerInput input, PlayerStateMachine stateMachine)
        {
            this.player = player;
            this.animator = animator;
            this.input = input;
            this.stateMachine = stateMachine;
        }

        public virtual void Enter()
        {
            stateStartTime = Time.time;
            animator.CrossFade(stateHash, transitionDuration);
        }

        public virtual void Exit()
        {
        }


        public virtual void LogicUpdate()
        {
        }

        public virtual void PhysicUpdate()
        {
        }
    }

}
